#!/bin/bash

FULLCOMMAND="$0 $@"
. ${BASEDIR}scripts/shflags

#define the flags
DEFINE_string 'robots' '50' 'The number of robots in the simulation' 'n';
DEFINE_integer 'seed' '-1' 'Seed' 'x';
DEFINE_string 'mu' '5' 'Mu' 'm'
DEFINE_string 'stepsPerCandidate' '900' 'Steps per candidate' 't'
DEFINE_string 'reEvaluationRate' '0.1' 'Re-evaluation Rate' 'r'
DEFINE_string 'crossoverRate' '1.0' 'Crossover rate' 'c'
DEFINE_string 'mutationRate' '1.0' 'Mutation rate' 'p'
DEFINE_string 'initialMutationStepSize' '5' 'Initial mutation step size' 's'
DEFINE_string 'newscastCacheSize' '4' 'Newscast cache size' 'C'
DEFINE_string 'newscastItemTTL' '8' 'Number of generations a Newscast item can live' 'L'
DEFINE_string 'numberOfParents' '2' 'Number of parents' 'P'
DEFINE_string 'iterations' '2000000' 'Number of iterations' 'i'
DEFINE_string 'numPlants' '180' 'Number of plants in the experiment' 'u'
DEFINE_string 'numTypes' '9' 'Number of type of plants' 'w'
DEFINE_string 'slope' '0' 'Slope at which the number of plants per type increases/decreases' 'l'
DEFINE_string 'cooling' 'true' 'If the simulation starts with the Cooling period' 'q'
DEFINE_string 'EnvironmentLifetime' '25' 'How many generation lasts the environment' 'el'
DEFINE_string 'StartTemperature' '20' 'Initial temperature for the simulation' 'y'
DEFINE_string 'MaxTemperature' '40' 'Min temperature reached by the environment' 'MT'
DEFINE_string 'MinTemperature' '0' 'Min temperature reached by the environment' 'mt'

DEFINE_string 'basedir' '/home/nico/workspace/nicolamularoni-dynamic/' 'Base dir of experiment' 'b'
DEFINE_string 'templatedir' 'scripts/templates/' 'd'
DEFINE_string 'confname' 'dynamic_p' 'o'
DEFINE_string 'logdir' 'logs' 'g'

# Parse the flags
FLAGS "$@" || exit 1
eval set -- "${FlAGS_ARGV}"

BASEDIR=${FLAGS_basedir}
TEMPLATEDIR=${FLAGS_templatedir}
CONFNAME=${FLAGS_confname}

RID=$RANDOM

### copy the template configuration to the config dir, making the neccesary adjustments
# Determine some properties of the test
ROBOTCOUNT=${FLAGS_robots}
TIMESTAMP=`date "+%Y%m%d.%Hh%Mm%Ss"`
LOGDIR=${FLAGS_logdir}
ESCAPEDLOGDIR=$(sed -e 's/[\/&]/\\&/g' <<< $LOGDIR)

# Determine where the configuration file will be placed
CONFDIR=${BASEDIR}config/
CONFFILE=${CONFDIR}${TIMESTAMP}.$RID.${TESTNAME}.$ROBOTCOUNT.properties
LOGFILE=${BASEDIR}/logs/console-output.${TIMESTAMP}.$RID.log
ERRORLOGFILE=${BASEDIR}/logs/console-error.${TIMESTAMP}.$RID.log

OUTPUTLOGFILE=${ESCAPEDLOGDIR}\\/output.${TIMESTAMP}.$RID.log
GENOMELOGFILE=${ESCAPEDLOGDIR}\\/genomes.${TIMESTAMP}.$RID.log
ORGANISMSIZESLOGFILE=${ESCAPEDLOGDIR}\\/organism-sizes.${TIMESTAMP}.$RID.log
ORGANISMLOGFILE=${ESCAPEDLOGDIR}\\/organism.${TIMESTAMP}.$RID.log
LOCATIONLOGFILE=${ESCAPEDLOGDIR}\\/locations.${TIMESTAMP}.$RID.log
FITNESSLOGFILE=${ESCAPEDLOGDIR}\\/fitness.${TIMESTAMP}.$RID.log

# Prepare the rules that will fill out the configuration file
SEEDREP=s/--RANDOMSEED/${FLAGS_seed:0:9}/g # extract only the first 9 decimals, because Roborobo can't handle int overflows
NBAGENTSREP=s/--NBAGENTS/${ROBOTCOUNT}/g
MUREP=s/--MU/${FLAGS_mu}/g
SPCREP=s/--STEPSPERCANDIDATE/${FLAGS_stepsPerCandidate}/g
RERREP=s/--REEVALUATIONRATE/${FLAGS_reEvaluationRate}/g
CRREP=s/--CROSSOVERRATE/${FLAGS_crossoverRate}/g
MRREP=s/--M-TATIONRATE/${FLAGS_mutationRate}/g
IMSSREP=s/--INITIALMUTATIONSTEPSIZE/${FLAGS_initialMutationStepSize}/g
NCCREP=s/--NEWSCASTCACHESIZE/${FLAGS_newscastCacheSize}/g
NCTTLREP=s/--NEWSCASTITEMTTL/${FLAGS_newscastItemTTL}/g
NOPREP=s/--NUMBEROFPARENTS/${FLAGS_numberOfParents}/g
ITERATIONREP=s/--ITERATIONS/${FLAGS_iterations}/g

NUMPLANTSREP=s/--NUMPLANTS/${FLAGS_numPlants}/g
NUMTYPESREP=s/--NUMTYPES/${FLAGS_numTypes}/g
SLOPEREP=s/--SLOPE/${FLAGS_slope}/g
COOLINGREP=s/--COOLING/${FLAGS_cooling}/g
ENVIRONMENTLIFETIMEREP=s/--ENVIRONMENTLIFETIME/${FLAGS_EnvironmentLifetime}/g
ENVIRONMENTTEMPREP=s/--STARTTEMP/${FLAGS_StartTemperature}/g
MAXTEMPREP=s/--MAXTEMP/${FLAGS_MaxTemperature}/g
MINTEMPREP=s/--MINTEMP/${FLAGS_MinTemperature}/g


OUTPUTLOGREP=s/--OUTPUTLOG/${OUTPUTLOGFILE}/g
GENOMELOGREP=s/--GENOMELOG/${GENOMELOGFILE}/g
ORGANISMSIZESREP=s/--ORGANISMSIZESLOG/${ORGANISMSIZESLOGFILE}/g
ORGANISMREP=s/--ORGANISMSLOG/${ORGANISMLOGFILE}/g
LOCATIONREP=s/--LOCATIONLOG/${LOCATIONLOGFILE}/g
FITNESSLOGREP=s/--FITNESSLOG/${FITNESSLOGFILE}/g


# Fill out and place the configuration file
sed -e $SEEDREP -e $NBAGENTSREP -e $MUREP -e $SPCREP -e $RERREP -e $CRREP -e $MRREP -e $IMSSREP -e $NCCREP -e $NCTTLREP -e $OUTPUTLOGREP -e $GENOMELOGREP -e $ORGANISMSIZESREP -e $ORGANISMREP -e $NOPREP -e $ITERATIONREP -e $LOCATIONREP -e $FITNESSLOGREP -e $NUMPLANTSREP -e $NUMTYPESREP -e $SLOPEREP -e $COOLINGREP -e $ENVIRONMENTLIFETIMEREP -e $ENVIRONMENTTEMPREP -e $MAXTEMPREP -e $MINTEMPREP ${BASEDIR}${TEMPLATEDIR}${CONFNAME}.template.properties > $CONFFILE

BINFILE=${BASEDIR}roborobo
$BINFILE -l $CONFFILE > $LOGFILE 2> $ERRORLOGFILE